package com.anycl.security.controller;


import com.anycl.security.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class LoginController {

    @Autowired
    private UserDetailsService myUserDetailsService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 登录并不会进入这个方法，不信debug试试
     */
    @PostMapping("/auth/login")
    public String login(String username, String password) throws Exception {
        User user = (User) myUserDetailsService.loadUserByUsername(username);
        if (!passwordEncoder.matches(password, user.getPassword()))
            throw new Exception("账号密码错误");
        return "/auth/login success";
    }

    @GetMapping("/logoutResult")
    public String logoutResult(){
        return "logout success";
    }

    @GetMapping("/session/invalid")
    public String sessionInvalid(){
        return "session invalid";
    }
}
